package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;

import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;

import com.cloudsigns.factorymanager2.project.myinerface.ItemCheckedChangeListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/5.
 */

public class MediaAdapter extends BaseAdapter {

    private Context context;
    private List<MediaBean> datas = new ArrayList<MediaBean>();
    private LayoutInflater inflater;
    private ItemClickListener itemClickListener;
    private ItemCheckedChangeListener itemCheckedChangeListener;
    private String tag = "";
    private String assignReceiveAccountId = "";


    public MediaAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;

    }

    public void setData(List<MediaBean> list) {

        if (null != list) {
            this.datas.clear();
            this.datas.addAll(list);
        }

    }

    public void setAssignReceiveAccountId(String assignReceiveAccountId) {
        this.assignReceiveAccountId = assignReceiveAccountId;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setItemCheckedChangeListener(ItemCheckedChangeListener itemCheckedChangeListener) {
        this.itemCheckedChangeListener = itemCheckedChangeListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public MediaBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_point_location_content, null);
            holder.cb = convertView.findViewById(R.id.cb);
            holder.tv_meida_name = convertView.findViewById(R.id.tv_meida_name);
            holder.tv_number = convertView.findViewById(R.id.tv_number);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MediaBean bean = datas.get(position);
        holder.tv_meida_name.setText(bean.getMediaName());
        holder.tv_number.setText(bean.getNumber());

        holder.cb.setChecked(bean.isChecked());
        holder.cb.setTag(bean);


        if (null != tag) {
            if (tag.equals("SelectMeidaForSuperActivity")) {
                if (bean.isEnable()) {
                    holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_000000));
                    holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_000000));
                    holder.cb.setClickable(true);
                    holder.cb.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (itemCheckedChangeListener != null) {
                                itemCheckedChangeListener.checkedChangeListener(view, position, holder.cb.isChecked());
                            }
                        }
                    });
                } else {
                    holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                    holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                    holder.cb.setClickable(false);
                }

            } else if (tag.equals("SelectMediaForManagerActivity")) {
                CommonEnum.AssignStatus assignStatus = bean.getAssignStatus();

                if (null != assignStatus) {
                    if (assignStatus == CommonEnum.AssignStatus.CREATED) {
                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_000000));
                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_000000));
                        if (bean.isChecked()) {
                            holder.cb.setVisibility(View.GONE);
                        } else {
                            holder.cb.setVisibility(View.VISIBLE);
//                        holder.cb.setClickable(true);
                            holder.cb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (itemCheckedChangeListener != null) {
                                        itemCheckedChangeListener.checkedChangeListener(view, position, holder.cb.isChecked());
                                    }
                                }
                            });
                        }

                    } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {
                        if (bean.isChecked()) {

                            holder.cb.setVisibility(View.GONE);


                        } else {
                            holder.cb.setVisibility(View.VISIBLE);
//                        holder.cb.setClickable(true);
                            holder.cb.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (itemCheckedChangeListener != null) {
                                        itemCheckedChangeListener.checkedChangeListener(view, position, holder.cb.isChecked());
                                    }
                                }
                            });
                        }
                    } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
//                        holder.cb.setClickable(false);
                        holder.cb.setVisibility(View.GONE);
                    } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
//                        holder.cb.setClickable(false);
                        holder.cb.setVisibility(View.GONE);
                    } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
//                        holder.cb.setClickable(false);
                        holder.cb.setVisibility(View.GONE);

//                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_000000));
//                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_000000));
//                        holder.cb.setClickable(true);
//                        holder.cb.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                if (itemCheckedChangeListener != null) {
//                                    itemCheckedChangeListener.checkedChangeListener(view, position, holder.cb.isChecked());
//                                }
//                            }
//                        });
                    } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
//                        holder.cb.setClickable(false);
                        holder.cb.setVisibility(View.GONE);
                    } else {
                        holder.tv_meida_name.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                        holder.tv_number.setTextColor(context.getResources().getColor(R.color.rgb_A6A6A6));
                        holder.cb.setVisibility(View.GONE);
                    }
                }
            }


        }


        return convertView;
    }

    private class ViewHolder {
        private TextView tv_meida_name;
        private TextView tv_number;
        private CheckBox cb;
    }
}
